// lib/models/delivery_info.dart
class DeliveryInfo {
  final String id; // 数据标识，对应 _id
  final String? owner; // 所有人，系统字段
  final int createdAt; // 创建时间，毫秒时间戳
  final String? createBy; // 创建人，系统字段
  final String address; // 收货地址
  final String phone; // 联系电话
  final String? updateBy; // 修改人，系统字段
  final List<String>? departmentList; // 所属部门，系统字段
  final String? openid; // 记录创建者，系统字段
  final String name; // 收货人
  final List<String> orders; // 关联的订单列表
  final int updatedAt; // 更新时间，毫秒时间戳

  DeliveryInfo({
    required this.id,
    this.owner,
    required this.createdAt,
    this.createBy,
    required this.address,
    required this.phone,
    this.updateBy,
    this.departmentList,
    this.openid,
    required this.name,
    required this.orders,
    required this.updatedAt,
  });

  factory DeliveryInfo.fromJson(Map<String, dynamic> json) {
    return DeliveryInfo(
      id: json['_id'] as String,
      owner: json['owner'] as String?,
      createdAt: json['createdAt'] as int? ?? 0,
      createBy: json['createBy'] as String?,
      address: json['address'] as String,
      phone: json['phone'] as String,
      updateBy: json['updateBy'] as String?,
      departmentList:
          (json['_departmentList'] as List<dynamic>?)?.cast<String>(),
      openid: json['_openid'] as String?,
      name: json['name'] as String,
      orders:
          (json['order'] as List<dynamic>?)
              ?.map((item) => (item as Map<String, dynamic>)['_id'] as String)
              .toList() ??
          [],
      updatedAt: json['updatedAt'] as int? ?? 0,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      '_id': id,
      'owner': owner,
      'createdAt': createdAt,
      'createBy': createBy,
      'address': address,
      'phone': phone,
      'updateBy': updateBy,
      '_departmentList': departmentList,
      '_openid': openid,
      'name': name,
      'order': orders.map((id) => {'_id': id}).toList(),
      'updatedAt': updatedAt,
    };
  }
}
